package com.itheima.reggie.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    //获取dao层对象
    @Autowired(required = false)
    private SetmealMapper setmealMapper;
    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;
    @Autowired(required = false)
    private CategoryMapper categoryMapper;


    /**
     * 添加套餐业务方法
     *
     * @param setmealDto
     * @return
     */
    @Transactional
    @Override
    public boolean save(SetmealDto setmealDto) {
        //完善数据
        setmealDto.setStatus(1);
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        //调用方法进行新增套餐 记得获取主键
        setmealMapper.save(setmealDto);

        //遍历套餐菜品关系列表完善数据
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes().stream().map((setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setSort(0);
            return setmealDish;
        })).collect(Collectors.toList());
        setmealDishMapper.save(setmealDishList);
        return true;
    }

    /**
     * 分页查询  --使用分表方式
     *
     * @param page
     * @param pageSize
     * @param name
     * @return Page<DishDto>
     */
    @Override
    public Page<SetmealDto> findSetmeal(Integer page, Integer pageSize, String name) {
        PageHelper.startPage(page, pageSize);
        List<Setmeal> setmeals = setmealMapper.findSetmeal(name);
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmeals);

        List<SetmealDto> setmealDtos = setmeals.stream().map((setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            Category category = categoryMapper.findById(setmealDto.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        })).collect(Collectors.toList());

        Page<SetmealDto> pageObj = new Page<>();
        pageObj.setRecords(setmealDtos);
        pageObj.setTotal(pageInfo.getTotal());

        return pageObj;
    }

    /**
     * 删除指定多个套餐id的套餐菜品关系数据(伪删除)
     * <p>
     * 记得开启事务
     *
     * @param ids
     * @param updateUser
     * @return
     */
    @Transactional
    @Override
    public boolean deleteByIds(List<Long> ids, Long updateUser) {
        Integer count = setmealMapper.countById(ids);
        if (count > 0) {
            throw new BusinessException("无法删除启售套餐");
        }
        //批量删套餐的菜品
        setmealDishMapper.deleteBySetmealIds(updateUser, LocalDateTime.now(), ids);
        //批量删除套餐
        setmealMapper.deleteByIds(updateUser, LocalDateTime.now(), ids);
        return true;
    }

    /**
     * 根据id查询一个套餐和多个套餐下的菜品
     *
     * @param id
     * @return SetmealDto
     */
    @Override
    public SetmealDto findById(Long id) {
        return setmealMapper.findById1(id);
    }

    /**
     * 获取列表数据
     *
     * @return list
     */
    @Override
    public List<Setmeal> findByCategoryId(Long categoryId) {
        return setmealMapper.findByCategoryId(categoryId);
    }
}
